
nums = [1,3,2]


var nextPermutation = function(nums) {
       let i=nums.length-2
       while(i>=0&&nums[i]>=nums[i+1])
       {
         i--
       }
       if(i>=0)
       {
         let j=nums.length-1
          while(j>0&&nums[j]<=nums[i])
        { 
         j--
        }
        [nums[i],nums[j]]=[nums[j],nums[i]]

       }
      
       const g=nums.splice(i+1)
       g.sort((a,b)=>a-b)
       nums.push(...g)
       return nums
};
console.log(nextPermutation(nums));
